< previous page page_630 next page >

Page 630
As you can see from the preceding example, a connection string can contain many different types of information, including user ID, password, and default database name.
Working with Recordsets
Recordsets represent actual data from a database. A recordset has fields (such as Name and Phone Number) and values for those fields (such as John Smith and 555-1212). Each set of field values that go together makes up a single record, and all these records together make up a recordset.
An easy way to visualize a recordset is as a spreadsheet or grid: Fields are like column names, and the rows are the records. Recordsets are usually created as the result of a query on the database. You use recordsets in your program to retrieve and update data.
In ADO, a recordset is stored in a Recordset object. After a Recordset object is populated with data, you can perform the following operations on the data:
Add new records
Edit existing records
Delete records
Navigate the recordset (change the current record)
Creating a Recordset
You have already learned how to create and populate a Recordset by using the Execute method of the Connection object. However, a Recordset object has its own methods and properties that can be used to retrieve data. As with all objects, to use these properties, you need to create a new instance of the Recordset object first:
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Next, you can use properties of the object to specify the connection, record source, and recordset type.
To specify a data source for a Recordset object, set the ActiveConnection property equal to an ADO Connection object:
rs.ActiveConnection = cn
The line of code assumes that cn represents an open connection that points to a data source, as discussed earlier. Another way to select a connection is to set the ActiveConnection property to the connection string:
rs.ActiveConnection = "DSN=BIBLIO"
If you use a string value instead of a Connection object, the Recordset object opens its own connection to the database.
A Recordset's Open method causes the recordset to be populated with data. To use the Open method, you must first set the Source property, which corresponds roughly to the RecordSource property of the ADO Data control. The code in Listing 28.1 creates a new Recordset object and then prints the data it contains.

 
< previous page page_630 next page >